package com.hrg.rds.biz.robot.controller;

import com.hrg.rds.biz.config.RdsConfiguration;
import com.hrg.rds.biz.robot.domain.AgvVo;
import com.hrg.rds.biz.robot.service.AgvService;
import com.hrg.sys.framework.web.controller.BaseController;
import com.hrg.sys.framework.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/open/agv")
public class AgvController extends BaseController {
    @Autowired
    private AgvService agvService;

    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult list() {
        List<AgvVo> agvList = agvService.selectList();
        return success(agvList);
    }

    @GetMapping(value = "/list_", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult list_() {
        return success(RdsConfiguration.AgvIpMap.values());
    }

    // 抢占控制权
    @GetMapping("/tryControl/{agvIp}")
    public AjaxResult tryControl(@PathVariable String agvIp) {
        return toAjax(agvService.tryControl(agvIp));
    }

    // 释放控制权
    @GetMapping("/releaseControl/{agvIp}")
    public AjaxResult releaseControl(@PathVariable String agvIp) {
        return toAjax(agvService.releaseControl(agvIp));
    }

    // 更新车辆状态
    @GetMapping("/status/{status}/{agvIp}")
    public AjaxResult status(@PathVariable int status, @PathVariable String agvIp) {
        //可接单、不接单是否占资源。默认不接单占资源
        return toAjax(agvService.status(status, agvIp));
    }
    
    @GetMapping("/map/download/{agvIp}")
    public AjaxResult downloadMap(@PathVariable String agvIp) {
        AgvVo agvVo = RdsConfiguration.AgvIpMap.get(agvIp);
        if (!agvVo.isIfConnectActive()) {
            error("AGV连接未激活");
        }
        agvService.downloadMap(agvIp);
        return success();
    }

}
